FastDFS Ngnix在Ubuntu下的配置

文章目录
  1. 1. FastDFS安装
  2. 2. 安装Ngix 和 fastdfs-nginx-module web服务器
  3. 3. 使用Python客户端上传测试
  4. 4. 查看上传文件

今天时间基本浪费在这个配置上了,踩了不少坑,留个记录,以便后续查阅。

首先说下今天遇到的第一个问题,我最初是使用Windows环境进行配置,fdfs的源文件中对于获取地址和端口的配置项存在问题,所以在配置过程中修改了不少原文件,无奈到了最后上传一步始终直困于无法连接本机的问题,故不再叙述安装过程。
下面阐述使用Ubuntu来搭建这个环境。

Ubuntu 版本:18.04

这里提供所有我用到安装文件:
链接:https://pan.baidu.com/s/1GLsNURrgR8jneLte_Bw61A
提取码:e9am

FastDFS安装

  1. 安装FastDFS依赖包
    解压缩libfastcommon-master.zip并进入到该目录,执行
    1
    2
    ./make.sh
    sudo ./make.sh install

2.安装fastdfs
解压缩fastdfs-master.zip并进入到该目录,执行

1
2
./make.sh
sudo ./make.sh install

3.配置跟踪服务器

1
sudo cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

在/home/python/目录中创建目录 fastdfs/tracker

1
mkdir -p /home/python/fastdfs/tracker

编辑/etc/fdfs/tracker.conf配置文件

1
2
vim /etc/fdfs/tracker.conf
修改 base_path=/home/python/fastdfs/tracker

4.配置存储服务器

1
sudo cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf

在/home/python/fastdfs/ 目录中创建目录 storage

1
mkdir –p /home/python/fastdfs/storage

编辑/etc/fdfs/storage.conf配置文件

1
2
3
4
5
sudo vim /etc/fdfs/storage.conf
修改:
base_path=/home/python/fastdfs/storage
store_path0=/home/python/fastdfs/storage
tracker_server=自己虚拟机ip:22122

5.启动tracker和storage

1
2
sudo service fdfs_trackerd start
sudo service fdfs_storaged start

没有返回消息即成功.

6.测试是否安装成功

1
sudo cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf

编辑/etc/fdfs/client.conf配置文件

1
2
3
4
sudo vim /etc/fdfs/client.conf
修改内容:
base_path=/home/python/fastdfs/tracker
tracker_server=自己ubuntu虚拟机ip:22122

上传文件测试:

1
fdfs_upload_file /etc/fdfs/client.conf 上传文件路径(比如 /home/ubuntu/Desktop/test.jpg)

如果返回结果 group1/M00/00 这样的信息就表示文件上传成功。

安装Ngix 和 fastdfs-nginx-module web服务器

1.解压缩 nginx-1.8.1.tar.gz 并进入到对应目录,执行

1
sudo ./configure --prefix=/usr/local/nginx/ --add-module=/home/willard/Fast-DFS/fastdfs-nginx-module-master/src

注意:这里的 –add-module 要用自己fastdfs-nginx-module-master的解压路径。

这里接下来需要执行

1
2
sudo ./make
sudo ./make install

然而这样会报错,执行不下去,这里需要先安装几个包:

1
2
sudo apt-get install libpcre3 libpcre3-dev 
sudo apt-get install zlib1g-dev

再执行

1
./configure

再打开 objs/Makefile文件
第三行有个 -Werror ,这里vim打开删掉即可。

执行完之后再执行:

1
2
sudo ./make
sudo ./make install

这里配置完之后,在objs/Makefile 里需要看到这条记录,否则 Ngix启动不起来

  1. 配置 mod_fastdfs.conf
    1
    sudo cp /home/willard/Ngix/fastdfs-nginx-module-master/src/mod_fastdfs.conf  /etc/fdfs/mod_fastdfs.conf

注意:前面的参数还是需要用自己本机解压缩的位置。

1
2
3
4
5
6
vim /etc/fdfs/mod_fastdfs.conf
修改内容:
connect_timeout=10
tracker_server=自己ubuntu的ip:22122
url_have_group_name=true
store_path0=/home/python/fastdfs/storage

3.配置http.conf

1
sudo cp /home/willard/Fast-DFS/fastdfs-master/conf/http.conf  /etc/fdfs/http.conf

注意:前面的参数也需要用自己本机解压缩的位置。

4.配置mime.types

1
sudo cp /home/willard/Fast-DFS/fastdfs-master/conf/mime.types /etc/fdfs/mime.types

5.配置 ngix.conf

1
vim /usr/local/nginx/conf/nginx.conf

在http部分添加如下配置:

1
2
3
4
5
6
7
8
9
10
11
server {
listen 8888;
server_name 虚拟机ip;
location ~/group[0-9] {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

6.启动 nginx

1
sudo /usr/local/nginx/sbin/nginx

还是那句话,没有信息就是安装成功了。

使用Python客户端上传测试

进入虚拟环境

1
workon dj01

进入fdfs_client-py-master.zip所在目录:

1
pip install fdfs_client-py-master.zip

测试代码

1
2
3
4
5
>>> from fdfs_client.client import Fdfs_client
>>>
>>> client = Fdfs_client('/etc/fdfs/client.conf')
>>>
>>> ret = client.upload_by_filename('/home/willard/Desktop/lewy.jpg')

结果如下即为成功:

查看上传文件

这里又是一个天坑,已经上传成功但是访问文件404.好了不废话了,这里直接给一个我测试过可以用的nginx.conf。
(1)添加了user root;
(2)删除了多余的location;
(3)添加了M00目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#user nobody;
user root;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 8888;
server_name 192.168.126.130;
location ~/group[0-9]/M00 {
root /etc/fdfs/storage/data;
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

修改完以后,进入nginx目录

1
2
3
4
cd /usr/local/nginx/sbin
./nginx -s stop
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
./nginx -s reload

再次访问:

1
http://192.168.126.130:8888/group1/M00/00/00/wKh-gl5lIt2AIdwxAA1R9JjVfKU654.jpg

关于问题排查,这里还有一份写得很清晰的博客:FastDFS 文件上传成功,访问404